#! /usr/bin/env python
# encoding: utf-8

import os, glob

######################
###### options #######
######################
def options (opt):
    grp_name = "cplex options (when --lp-lib=cplex is used)"
    grp = opt.add_option_group (grp_name)
    grp.add_option ("--cplex-path", action="store", type="string",
                                    dest="CPLEX_PATH", default = "",
                    help = "location of the cplex lib and include directories \
                            (by default use the one in 3rd directory)")
    grp.add_option ("--cplex-version", action="store", type="string",
                                    dest="CPLEX_VERSION", default = "",
                    help = "Version of CPLEX (e.g. for version 12.9.0, use 1290)")
######################
##### configure ######
######################
def configure (conf):
    if conf.env["LP_LIB"]:
        conf.fatal ("Trying to configure a second library for LP")
    conf.env["LP_LIB"] = "CPLEX"

    conf.define("IL_STD", 1)

    cplex_dir = os.path.join(conf.options.CPLEX_PATH, "cplex")
    cplex_version = conf.options.CPLEX_VERSION
    has_cplex1 = False
    has_cplex2 = False
    if cplex_dir:
        cplex_include = os.path.join (cplex_dir, "include")
        cplex_libtest = glob.glob(os.path.join (cplex_dir, "lib/*/*/libcplex*") )
        cplex_dylibtest = glob.glob(os.path.join (cplex_dir, "bin/*/libcplex{}*".format(cplex_version)) )
        if cplex_libtest:
            cplex_lib = os.path.abspath(os.path.dirname(cplex_libtest[0]))
            cplex_dylib = os.path.abspath(os.path.dirname(cplex_dylibtest[0]))
        else:
            cplex_lib = os.path.join (cplex_dir, "lib/x86-64_linux/static_pic")
            cplex_dylib = os.path.join (cplex_dir, "bin/x86-64_linux")

        conf.msg ("Using library CPLEX from", cplex_dir)
        has_cplex1 = conf.check_cxx (header_name = "ilcplex/cplex.h", includes = cplex_include,
                use = ["IBEX", "LP_LIB"], uselib_store = "LP_LIB", mandatory = False)
        has_cplex2 = conf.check_cxx (lib = ["cplex", "ilocplex", "pthread"], libpath = cplex_lib,
                use = ["IBEX", "LP_LIB"], uselib_store = "LP_LIB", mandatory = False)
        has_cplex3 = conf.check_cxx (lib = ["cplex{}".format(cplex_version)], libpath = cplex_dylib,
                use = ["IBEX", "LP_LIB"], uselib_store = "LP_LIB", mandatory = False)
        conf.env.append_unique ("INCLUDES_IBEX_DEPS", cplex_include)
        conf.env.append_unique ("LIBPATH_IBEX_DEPS", cplex_lib)
        conf.env.append_unique ("LIBPATH_IBEX_DEPS", cplex_dylib)
        conf.env.append_value("LDFLAGS", "-Wl,-rpath,{}".format(cplex_dylib))
    if (not(has_cplex1) or not(has_cplex2)) :
        conf.fatal ('CPLEX is not found. Use the option --cplex-path="your_path_to_cplex" ')

    # Concert technology lib
    concert_dir = os.path.join(conf.options.CPLEX_PATH, "concert")
    has_concert1 = False
    has_concert2 = False
    if concert_dir:
        concert_include = os.path.join (concert_dir, "include")

        concert_libtest = glob.glob(os.path.join (concert_dir, "lib/*/*/libconcert*") )
        if concert_libtest:
            concert_lib = os.path.abspath(os.path.dirname(concert_libtest[0]))
        else:
            concert_lib = os.path.join (concert_dir, "lib/x86-64_linux/static_pic")

        conf.msg ("Using library Concert from", concert_dir)
        has_concert1 = conf.check_cxx (header_name = "ilconcert/ilomodel.h", includes = concert_include,
                use = ["IBEX", "LP_LIB"], uselib_store = "LP_LIB", mandatory =False)
        has_concert2 = conf.check_cxx (lib = ["concert", "pthread"], libpath = concert_lib,
                use = ["IBEX", "LP_LIB"], uselib_store = "LP_LIB", mandatory =False)

        conf.env.append_unique ("INCLUDES_IBEX_DEPS", concert_include)
        conf.env.append_unique ("LIBPATH_IBEX_DEPS", concert_lib)

    if (not(has_concert1) or not(has_concert2)) :
        conf.fatal ('CPLEX concert library is not found. Use the option --cplex-path="your_path_to_cplex" ')
